﻿using Management.Auxiliary;
using Management.Core.Team;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;

namespace Management.Areas.Control.Controllers
{
    [AdminAuthorize]
    public class TeamController : Controller
    {
        private TeamManager tm = new TeamManager();
        //
        // GET: /Control/Team/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="teamId"></param>
        /// <param name="name"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public ActionResult PageList(int? teamId, string name, int? pageNumber, int? pageSize, int? order)
        {
            Paging<Team> _pagingTeam = new Paging<Team>();
            if (pageNumber != null && pageNumber > 0) _pagingTeam.PageIndex = (int)pageNumber + 1;
            if (pageSize != null && pageSize > 0) _pagingTeam.PageSize = (int)pageSize;
            var _paging = tm.FindPageList(_pagingTeam, teamId, name, null);
            return Json(new { total = _paging.TotalNumber, rows = _paging.Items });
        }

        /// <summary>
        /// 增加班组
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            var _workshops = new Management.Core.Workshop.WorkshopManager().FindList();
            List<SelectListItem> _listItems = new List<SelectListItem>(_workshops.Count());
            foreach (var workshop in _workshops)
            {
                _listItems.Add(new SelectListItem() { Text = workshop.Name, Value = workshop.WorkshopID.ToString() });
            }
            ViewBag.Workshops = _listItems;
            return PartialView();
        }

        /// <summary>
        /// 增加班组
        /// </summary>
        /// <param name="_team"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Add([Bind(Include = "TeamID, Name, Leader, Number, WorkshopID, DepartmentID, ProcessID, ManufacturingID, Created_at, Updated_at")]Team _team)
        {
            _team.Created_at = DateTime.Now;
            _team.Updated_at = DateTime.Now;
            Response _resp = new Response() { Code = -1 };
            if (ModelState.IsValid)
            {
                //检查名称
                if (tm.IsContains(u => u.Name == _team.Name))
                {
                    _resp.Code = 0;
                    _resp.Message = "班组存在";
                }
                else
                {
                    _resp = tm.Add(_team);
                }
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = General.GetModelErrorString(ModelState);
            }
            return Json(_resp);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="teamid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(int teamid)
        {
            Response _resp = new Response();
            var _team = tm.Find(teamid);
            if (_team == null)
            {
                _resp.Code = 0;
                _resp.Message = "要删除条目不存在";
            }
            else
            {
                _resp = tm.Delete(_team);
            }
            return Json(_resp);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Modify(int id)
        {
            var _workshops = new Management.Core.Workshop.WorkshopManager().FindList();
            List<SelectListItem> _listItems = new List<SelectListItem>(_workshops.Count());
            foreach (var workshop in _workshops)
            {
                _listItems.Add(new SelectListItem() { Text = workshop.Name, Value = workshop.WorkshopID.ToString() });
            }
            ViewBag.Workshops = _listItems;
            return PartialView(tm.Find(id));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Modify(int id, FormCollection form)
        {
            Response _resp = new Response();
            var _team = tm.Find(id);
            _team.Updated_at = DateTime.Now;
            if (TryUpdateModel(_team, new string[] { "Name", "Leader", "Number", "WorkshopID", "DepartmentID", "ProcessID", "ManufacturingID", "Created_at", "Updated_at" }))
            {
                if (tm.IsContains(u => u.Name == _team.Name) && form["Name"] != _team.Name)
                {
                    _resp.Code = 0;
                    _resp.Message = "名称已存在";
                }
                else
                {
                    _resp = tm.Update(_team);
                }
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = "模型不符合条件，更新失败";
            }
            return Json(_resp);
        }
	}
}